<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="500px"
      append-to-body
      :before-close="close"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="120px">
        <el-form-item label="处理凭证" prop="payVoucher">
          <image-upload v-model="form.payVoucher" :limit="9"/>
        </el-form-item>
      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { debounce } from '@/utils'
import { proprietorBillOfflinePayment } from '@/api/bill/proprietor'

export default {
  props: {
    value: {
      type: Boolean,
      default: false
    },
    title: {
      default: '账单详情'
    },
    data: {
      default: () => ({})
    }
  },
  dicts: ['pay_state'],
  data() {
    return {
      // 表单参数
      form: {},
      voucherVisible: false,
      rules: {
        payVoucher: [
          { required: true, message: '请上传处理凭证', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val) // 此处监测showMask目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
      }
    }
  },
  watch: {
    value: {
      handler(newVal, oldVal) {
        if (newVal) {
          this.init()
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    init() {
      this.reset()
    },
    //重置表单
    reset() {
      this.form = {
        itemId: undefined, // 项目id
        name: ''//名称
      }
      this.resetForm('form')
      if (this.data.proprietorBillId) {
        this.form = this.data
      }

    },
    submit: debounce(function() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            proprietorBillOfflinePayment(this.form).then(res => {
              this.$message.success(res.msg || '提交成功')
              this.$emit('success')
              this.close()
            }).catch(err => {
              this.$message.error(err.msg || '提交失败')
            })
          } else {
            return false
          }
        })
      }, 500, true
    ),
    close() {
      this.reset()
      this.visible = false
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  max-height: 80vh !important;
  overflow-y: auto;
}

</style>
